package handler

import (
	v1 "gin-vben-admin/api/v1"
	"gin-vben-admin/api/v1/errcode"
	"gin-vben-admin/internal/service"
	"github.com/gin-gonic/gin"
	"strconv"
)

type SysMenuHandler struct {
	*Handler
	sysMenuService service.SysMenuService
}

func NewSysMenuHandler(handler *Handler, sysMenuService service.SysMenuService) *SysMenuHandler {
	return &SysMenuHandler{
		Handler:        handler,
		sysMenuService: sysMenuService,
	}
}

func (h *SysMenuHandler) GetTreeList(ctx *gin.Context) {
	var req v1.SysMenuListReq
	if err := ctx.ShouldBind(&req); err != nil {
		v1.Error(ctx, err)
		return
	}
	data, err := h.sysMenuService.GetTreeListByPid(ctx, req.MenuId)
	v1.Response(ctx, err, data)
}

func (h *SysMenuHandler) Detail(ctx *gin.Context) {
	_id := ctx.Param("id")
	id, err := strconv.ParseInt(_id, 10, 64)
	if err != nil {
		v1.Error(ctx, errcode.ErrBadRequest)
		return
	}
	data, err := h.sysMenuService.Detail(ctx, id)
	v1.Response(ctx, err, data)
}

func (h *SysMenuHandler) Create(ctx *gin.Context) {
	var req = v1.SysMenuCreateReq{AuthId: UserId(ctx)}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.Error(ctx, err)
		return
	}
	v1.Response(ctx, h.sysMenuService.CreateMenu(ctx, &req), nil)
}

func (h *SysMenuHandler) Update(ctx *gin.Context) {
	var req = v1.SysMenuUpdateReq{AuthId: UserId(ctx)}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		v1.Error(ctx, err)
		return
	}
	v1.Response(ctx, h.sysMenuService.UpdateMenu(ctx, &req), nil)
}

func (h *SysMenuHandler) CreateAction(ctx *gin.Context) {
	var req = v1.SysMenuCreateActionReq{AuthId: UserId(ctx)}
	if err := ctx.BindJSON(&req); err != nil {
		v1.Error(ctx, err)
		return
	}
	v1.Response(ctx, h.sysMenuService.CreateAction(ctx, &req), nil)
}

func (h *SysMenuHandler) UpdateAction(ctx *gin.Context) {
	var req = v1.SysMenuUpdateActionReq{AuthId: UserId(ctx)}
	if err := ctx.BindJSON(&req); err != nil {
		v1.Error(ctx, err)
		return
	}
	v1.Response(ctx, h.sysMenuService.UpdateAction(ctx, &req), nil)
}

func (h *SysMenuHandler) Delete(ctx *gin.Context) {
	_id := ctx.Param("id")
	id, err := strconv.ParseInt(_id, 10, 64)
	if err != nil {
		v1.Error(ctx, errcode.ErrBadRequest)
		return
	}
	v1.Response(ctx, h.sysMenuService.Delete(ctx, id), nil)
}
